package cn.itcast.fore.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.utils.Constants;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order>{
	
	
	private String sendAreaInfo;
	private String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value="saveOrder",results={@Result(name="success",type="redirect",location="index.html#/")})	
	public String saveOrder(){
		
	String[] strings = sendAreaInfo.split("/");
	Area sendArea = new Area();
	sendArea.setProvince(strings[0]);
	sendArea.setCity(strings[1]);
	sendArea.setDistrict(strings[2]);
	String[] strings2 = recAreaInfo.split("/");
	Area recArea = new Area();
	recArea.setProvince(strings2[0]);
	recArea.setCity(strings2[1]);
	recArea.setDistrict(strings2[2]);
	model.setSendArea(sendArea);
	model.setRecArea(recArea);
	
	Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
	model.setCustomer_id(customer.getId());
	WebClient.create(Constants.getBosManagementUrl()+"/bos_management/services/orderService/saveOrder").type(MediaType.APPLICATION_JSON).post(model);
		
	return SUCCESS;
	}
}
